#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setFixedSize(this->geometry().size());//Qt设置窗口不能更改大小
    this->setWindowTitle("厦门科盛金属升级工具");

    debug_crc=new CRC;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_btn_MakeFirmware_clicked()
{
    char  buff[128+6];
    QByteArray arr;
    QByteArray firmwarearr;
    uint16_t total_packet_count = 0x22;
    uint16_t packet_index = 0;
    int i = 0;
    int j = 0;
    int k = 0;

    QFile file;
    QString path = QFileDialog::getOpenFileName(this, "打开", "../", "bin(*.bin)");
    QFile srcfile(path);
    bool b_srcopen = srcfile.open(QIODevice::ReadOnly);
    if (b_srcopen == true)
    {
        //获取原固件大小
        qint64 srcfilesize = srcfile.size();

        if(srcfilesize % 128  != 0)
        {
            total_packet_count = srcfilesize / 128 + 1;
        }
        else
        {
            total_packet_count = srcfilesize;
        }
        //读取原固件
        arr = srcfile.readAll();

        //关联文件名字
        QFileInfo fileinfo = QFileInfo(path);
        //QString file_name = fileinfo.fileName();
        QString file_path = fileinfo.path();
        //QString FirmwareName = "\\Firmware.bin";
        QString FirmwareName = "\\Firmware_";
        QDateTime timeCur = QDateTime::currentDateTime();
        QString timeStr = timeCur.toString("yyyy-MM-dd_ver");
        FirmwareName += timeStr;
        FirmwareName += ui->le_ver->text();
        FirmwareName += "_type";
        FirmwareName += ui->le_devtype->text();
        FirmwareName += ".bin";

        file.setFileName(file_path + FirmwareName);

        bool b_open = file.open(QIODevice::WriteOnly);

        if (b_open == true)
        {
            for(k = 0; k < total_packet_count; k++)
            {
                packet_index = k;
                memset(buff, 0, sizeof(buff));
                buff[0] = (uint8_t)((ui->le_ver->text().toInt())>>8);
                buff[1] = (uint8_t)((ui->le_ver->text().toInt())&0xff);  //版本号
                buff[2] = (uint8_t)((ui->le_devtype->text().toInt())>>8);
                buff[3] = (uint8_t)((ui->le_devtype->text().toInt())&0xff);
                buff[4] = (uint8_t)(total_packet_count >> 8);
                buff[5] = (uint8_t)(total_packet_count & 0xff);
                buff[6] = (uint8_t)(packet_index >> 8);
                buff[7] = (uint8_t)(packet_index & 0xff);

                for(i = 0; i < 128; i++)
                {
                    if((j + i) < srcfilesize)
                        buff[8 + i] = arr[j + i];
                    else
                    {
                        buff[8 + i] = 0xff;
                    }
                }
                j += 128;
                //packet_index++;
                firmwarearr.append(buff, 128+8);
                file.write(buff, 128+8);
               }

            //firmwarearr.to
            //char *tt = firmwarearr.data();
            //int aa = firmwarearr.size();
            //uint16_t crc = debug_crc->Get_Crc16(firmwarearr.data(),firmwarearr.size());
            /*send_ask[k++]=(uint8_t)crc;//crc16
            send_ask[k++]=(uint8_t)(crc>>8);//crc16

            QByteArray Send_read_region((char*)send_ask,k);*/
            //file.write(str.toUtf8());
        }
        file.close();
    }

    srcfile.close();


    QString content;
    while (!file.atEnd()) {
        QByteArray line = file.readLine();
        content += QString::fromUtf8(line);
    }

    // 关闭文件
    file.close();

    QString srcfileName = srcfile.fileName(); // 源文件目录名字
    ui->src_file_dir->setText(srcfileName);

    QString fileName = file.fileName(); // 生成文件目录名字
    ui->file_dir->setText(fileName);

    ui->label_total_packet_count->setText( QString::number(total_packet_count) );
    ui->label_total_packet_count_time->setText( QString::number(total_packet_count*3/60.0) );
/*
    //关联文件名字
    QFileInfo fileinfo = QFileInfo(path);
    //QString file_name = fileinfo.fileName();
    QString file_path = fileinfo.path();
    QString FirmwareName = "\\Firmware.bin";

    file.setFileName(file_path + FirmwareName);

    bool b_open = file.open(QIODevice::WriteOnly);

    if (b_open == true)
    {
        //QFile只支持UTF-8格式
        QString str = "ui->textEdit->toPlainText()";
        file.write(str.toUtf8());
    }
    file.close();*/

}



